package NetModel.network;


import android.util.Log;

import java.io.IOException;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2016/10/27.
 */
public abstract class Backgrounder {

    private static final String TAG = "Backgrounder";

    //    private static ExecutorService executorService = Executors.newSingleThreadExecutor();
    private static ThreadPoolExecutor executorService = new ThreadPoolExecutor(1, 1,
            0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(1));

    {
        executorService.setRejectedExecutionHandler(new RejectedExecutionHandler() {
            @Override
            public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
                Log.e(TAG, "Task " + r.toString() + " rejected from " + e.toString());
            }
        });
    }

    public void execute() {
        executorService.execute(new Task());
    }

    protected abstract void doTask() throws ClientException, IOException;

    private class Task implements Runnable {
        @Override
        public void run() {
            //token检查
            try {
                doTask();
            } catch (ClientException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


}
